//
//  XBookMarkPage.m
//  food
//
//  Created by XmaMini on 2019/5/14.
//  Copyright © 2019 XmaMini. All rights reserved.
//

#import "XBookMarkPage.h"
#import "ReadTool.h"
#import "XInfoPage.h"



@implementation XBookMarkView
- (void)updateCellAppearanceWithIndexPath:(NSIndexPath *)indexPath {
    [super updateCellAppearanceWithIndexPath:indexPath];

    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.mImageView.bounds cornerRadius:QMUICMI.searchBarTextFieldCornerRadius];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init];
    //设置大小
    maskLayer.frame = self.mImageView.bounds;
    //设置图形样子
    maskLayer.path = maskPath.CGPath;
    self.mImageView.layer.mask = maskLayer;
}



- (void)setFrame:(CGRect)frame
{
    CGRect bounds = [UIScreen mainScreen].bounds;
    frame.origin.x = 10;
    frame.size.width=bounds.size.width - frame.origin.x *2;
    [super setFrame:frame];
}
@end


@interface XBookMarkPage ()

@end

@implementation XBookMarkPage


- (void)initSubviews{
    [super initSubviews];
    NSDictionary *config =[ReadTool readLocalFileWithName:@"config"];
    if(config!=nil){
        self.config =[config objectForKey:@"index"];
    }
    self.title = @"我的收藏";
    [self updateData];
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if(self.bookmark){
        return self.bookmark.count;
    }
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 15;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 180;
}


//去掉UItableview headerview黏性(sticky)
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView == self.tableView)
    {
        CGFloat sectionHeaderHeight = 10; //sectionHeaderHeight
        if (scrollView.contentOffset.y <= sectionHeaderHeight && scrollView.contentOffset.y >= 0) {
            scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
        } else if (scrollView.contentOffset.y >= sectionHeaderHeight) {
            scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
        }
    }
}

//将图片处理成圆的
-(id)changeimagetocilce:(UIImage*)image
{
    
    UIImage *finalImage = nil;
    UIGraphicsBeginImageContext(image.size);
    {
        //        UIGraphicsBeginImageContext(CGSizeMake(100,100));
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        CGAffineTransform trnsfrm = CGAffineTransformConcat(CGAffineTransformIdentity, CGAffineTransformMakeScale(1.0, -1.0));
        trnsfrm = CGAffineTransformConcat(trnsfrm, CGAffineTransformMakeTranslation(0.0, image.size.height));
        CGContextConcatCTM(ctx, trnsfrm);
        CGContextBeginPath(ctx);
        CGContextAddEllipseInRect(ctx, CGRectMake(0.0, 0.0, image.size.width, image.size.height));
        CGContextClip(ctx);
        CGContextDrawImage(ctx, CGRectMake(0.0, 0.0, image.size.width, image.size.height), image.CGImage);
        finalImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    }
    
    return finalImage;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *NODE_CELL_ID = @"IndexTableViewCellIdentifier";
    XBookMarkView * cell = [tableView dequeueReusableCellWithIdentifier:NODE_CELL_ID];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"XIndexItem" owner:nil options:nil] firstObject];
    }
    
    cell = [[[NSBundle mainBundle] loadNibNamed:@"XIndexItem" owner:nil options:nil] firstObject];
    NSDictionary *dict = nil;
    for(int i = 0;i<self.config.count;i++){
        dict = [self.config objectAtIndex:i];
        if([[dict valueForKey:@"name"] isEqual:[self.bookmark objectAtIndex:self.bookmark.count - indexPath.section - 1]]){
            break;
        }
    }
    
    cell.mImageView.image =  [self changeimagetocilce:[UIImage imageNamed:[dict valueForKey:@"img"]]];
    cell.mName.text = [dict valueForKey:@"name"];
    cell.mDesc.text = [dict valueForKey:@"desc"];
    cell.mName.textColor = QMUICMI.tabBarTintColor;
    cell.mDesc.textColor = QMUICMI.tabBarTintColor;
    [cell updateCellAppearanceWithIndexPath:indexPath];
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    XInfoPage *viewController = [[XInfoPage alloc] init];
    [viewController setConfig:[self.config objectAtIndex:indexPath.row]];
    [self.navigationController pushViewController:viewController animated:YES];
    
}

-(void)updateData{
    NSMutableArray *likeArray = [[[NSUserDefaults standardUserDefaults]objectForKey:@"bookmark"] mutableCopy];
    if(likeArray){
        self.bookmark = likeArray ;
    }else{
        self.bookmark = nil;
    }
    [self.tableView reloadData];
}

@end
//
//
//@implementation XIndexPage
//
//
//- (void)initSubviews{
//    [super initSubviews];
//    NSDictionary *config =[ReadTool readLocalFileWithName:@"config"];
//    if(config!=nil){
//        self._config =[config objectForKey:@"index"];
//        self._title = [config objectForKey:@"title"];
//
//        if(self.showType != 0){
//            self.showCount = 0;
//            for(int i = 0;i<self._config.count;i++){
//                int type = [[[self._config objectAtIndex:i] valueForKey:@"type"] intValue];
//                if(type == (self.showType - 1)){
//                    self.showCount ++;
//                }
//            }
//        }else{
//            self.showCount = (int)self._config.count;
//        }
//    }
//    self.title = self._title;
//
//    if(self.showType == 1){
//        self.title = @"早餐";
//    }else if(self.showType == 2){
//        self.title = @"午餐";
//    }else if(self.showType == 3){
//        self.title = @"晚餐";
//    }
//}
//
////- (void)viewDidLoad{
////    [super viewDidLoad];
////    NSLog(@"viewDidLoad  showtype = %d",self.showType);
////
////}
//
//
//
//
////- (void)setupNavigationItems {
////    [super setupNavigationItems];
////    self.title = self._title;
////}
//
//- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//    return 1;
//}
//
//- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//    //    return self._config.count;
//
//    return self.showCount;
//}
//
//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
//    return 15;
//}
//
//
//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//    return 180;
//}
//
//
////去掉UItableview headerview黏性(sticky)
//- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
//    if (scrollView == self.tableView)
//    {
//        CGFloat sectionHeaderHeight = 10; //sectionHeaderHeight
//        if (scrollView.contentOffset.y <= sectionHeaderHeight && scrollView.contentOffset.y >= 0) {
//
//            scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
//
//        } else if (scrollView.contentOffset.y >= sectionHeaderHeight) {
//
//            scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
//
//        }
//    }
//}
//
//
//
//- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//    static NSString *NODE_CELL_ID = @"IndexTableViewCellIdentifier";
//    XIndexItemView * cell = [tableView dequeueReusableCellWithIdentifier:NODE_CELL_ID];
//    if (!cell) {
//        cell = [[[NSBundle mainBundle] loadNibNamed:@"XIndexItem" owner:nil options:nil] firstObject];
//    }
//
//    cell = [[[NSBundle mainBundle] loadNibNamed:@"XIndexItem" owner:nil options:nil] firstObject];
//
//    NSDictionary *dict = [self._config objectAtIndex:indexPath.section];
//    if(self.showType != 0){
//        int index = 0;
//        for(int i = 0;i<self._config.count;i++){
//            dict = [self._config objectAtIndex:i];
//            int type = [[dict valueForKey:@"type"] intValue];
//            if(type == (self.showType - 1)){
//                if(index == indexPath.section){
//                    [cell setTag:i];
//                    break;
//                }
//                index ++;
//            }
//        }
//    }
//
//    cell.mImageView.image =  [self changeimagetocilce:[UIImage imageNamed:[dict valueForKey:@"img"]]];
//    cell.mName.text = [dict valueForKey:@"name"];
//    cell.mDesc.text = [dict valueForKey:@"desc"];
//    cell.mName.textColor = QMUICMI.tabBarTintColor;
//    cell.mDesc.textColor = QMUICMI.tabBarTintColor;
//    [cell updateCellAppearanceWithIndexPath:indexPath];
//
//    return cell;
//}
//
//
//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//    [tableView deselectRowAtIndexPath:indexPath animated:NO];
//
//
//    XInfoPage *viewController = [[XInfoPage alloc] init];
//    [viewController setConfig:[self._config objectAtIndex:indexPath.row]];
//    [self.navigationController pushViewController:viewController animated:YES];
//
//}
//
//@end

